package savantConn;

import java.util.logging.Logger;
import org.redisson.Redisson;
import org.redisson.api.RAtomicLong;
import org.redisson.api.RBucket;
import org.redisson.api.RCountDownLatch;
import org.redisson.api.RDeque;
import org.redisson.api.RList;
import org.redisson.api.RLock;
import org.redisson.api.RMap;
import org.redisson.api.RQueue;
import org.redisson.api.RSet;
import org.redisson.api.RSortedSet;
import org.redisson.api.RTopic;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;

public class RedisTool {

  public static void main(String[] args) {
    Jedis jedis = new Jedis("127.0.0.1", 6379);
    System.out.println(jedis.ping());
    jedis.set("xxx", "aaa");
    System.out.println(jedis.get("xxx"));
  }

  static class redissionUtils {

    private static Logger logger = (Logger) LoggerFactory.getLogger(redissionUtils.class);
    private static redissionUtils redisUtils;


    private redissionUtils() {
    }

    /**
     * redisson多种连接方式
     */
    public static void main(String[] args) {
      //单机
      Config config = new Config();
      config.useSingleServer().setAddress("myredisserver:6379");
      RedissonClient redisson = Redisson.create(config);

      //主从

      //      Config config = new Config();
      //      config.useMasterSlaveServers()
      //          .setMasterAddress("127.0.0.1:6379")
      //          .addSlaveAddress("127.0.0.1:6389", "127.0.0.1:6332", "127.0.0.1:6419")
      //          .addSlaveAddress("127.0.0.1:6399");
      //      RedissonClient redisson = Redisson.create(config);

      //哨兵
      //      Config config = new Config();
      //      config.useSentinelServers()
      //          .setMasterName("mymaster")
      //          .addSentinelAddress("127.0.0.1:26389", "127.0.0.1:26379")
      //          .addSentinelAddress("127.0.0.1:26319");
      //      RedissonClient redisson = Redisson.create(config);

      //集群
      //      Config config = new Config();
      //      config.useClusterServers()
      //          .setScanInterval(2000) // cluster state scan interval in milliseconds
      //          .addNodeAddress("127.0.0.1:7000", "127.0.0.1:7001")
      //          .addNodeAddress("127.0.0.1:7002");
      //      RedissonClient redisson = Redisson.create(config);
    }

    /**
     * 提供单例模式
     *
     * @return
     */
    public static redissionUtils getInstance() {
      if (redisUtils == null) {
        synchronized (redissionUtils.class) {
          if (redisUtils == null) {
            redisUtils = new redissionUtils();
          }
        }
      }
      return redisUtils;
    }

    /**
     * 使用config创建Redisson Redisson是用于连接Redis Server的基础类
     *
     * @param config
     * @return
     */
    public RedissonClient getRedisson(Config config) {
      RedissonClient redisson = Redisson.create(config);
      logger.info("成功连接Redis Server");
      return redisson;
    }

    /**
     * 使用ip地址和端口创建Redisson
     *
     * @param ip
     * @param port
     * @return
     */
    public RedissonClient getRedisson(String ip, String port) {
      Config config = new Config();
      config.useSingleServer().setAddress(ip + ":" + port);
      RedissonClient redisson = Redisson.create(config);
      logger.info("成功连接Redis Server" + "\t" + "连接" + ip + ":" + port + "服务器");
      return redisson;
    }

    /**
     * 关闭Redisson客户端连接
     *
     * @param redisson
     */
    public void closeRedisson(RedissonClient redisson) {
      redisson.shutdown();
      logger.info("成功关闭Redis Client连接");
    }

    /**
     * 获取字符串对象
     *
     * @param redisson
     * @param objectName
     * @return
     */
    public <T> RBucket<T> getRBucket(RedissonClient redisson, String objectName) {
      RBucket<T> bucket = redisson.getBucket(objectName);
      return bucket;
    }

    /**
     * 获取Map对象
     *
     * @param redisson
     * @param objectName
     * @return
     */
    public <K, V> RMap<K, V> getRMap(RedissonClient redisson, String objectName) {
      RMap<K, V> map = redisson.getMap(objectName);
      return map;
    }

    /**
     * 获取有序集合
     *
     * @param redisson
     * @param objectName
     * @return
     */
    public <V> RSortedSet<V> getRSortedSet(RedissonClient redisson, String objectName) {
      RSortedSet<V> sortedSet = redisson.getSortedSet(objectName);
      return sortedSet;
    }

    /**
     * 获取集合
     *
     * @param redisson
     * @param objectName
     * @return
     */
    public <V> RSet<V> getRSet(RedissonClient redisson, String objectName) {
      RSet<V> rSet = redisson.getSet(objectName);
      return rSet;
    }

    /**
     * 获取列表
     *
     * @param redisson
     * @param objectName
     * @return
     */
    public <V> RList<V> getRList(RedissonClient redisson, String objectName) {
      RList<V> rList = redisson.getList(objectName);
      return rList;
    }

    /**
     * 获取队列
     *
     * @param redisson
     * @param objectName
     * @return
     */
    public <V> RQueue<V> getRQueue(RedissonClient redisson, String objectName) {
      RQueue<V> rQueue = redisson.getQueue(objectName);
      return rQueue;
    }

    /**
     * 此方法不可用在Redisson 1.2 中 在1.2.2版本中 可用
     *
     * @param redisson
     * @param objectName
     * @return
     */
    /**
     * public <V> RBlockingQueue<V> getRBlockingQueue(RedissonClient
     * redisson,String objectName){ RBlockingQueue
     * rb=redisson.getBlockingQueue(objectName); return rb; }
     */

    /**
     * 获取双端队列
     *
     * @param redisson
     * @param objectName
     * @return
     */
    public <V> RDeque<V> getRDeque(RedissonClient redisson, String objectName) {
      RDeque<V> rDeque = redisson.getDeque(objectName);
      return rDeque;
    }

    /**
     * 获取锁
     *
     * @param redisson
     * @param objectName
     * @return
     */
    public RLock getRLock(RedissonClient redisson, String objectName) {
      RLock rLock = redisson.getLock(objectName);
      return rLock;
    }

    /**
     * 获取原子数
     *
     * @param redisson
     * @param objectName
     * @return
     */
    public RAtomicLong getRAtomicLong(RedissonClient redisson, String objectName) {
      RAtomicLong rAtomicLong = redisson.getAtomicLong(objectName);
      return rAtomicLong;
    }

    /**
     * 获取记数锁
     *
     * @param redisson
     * @param objectName
     * @return
     */
    public RCountDownLatch getRCountDownLatch(RedissonClient redisson, String objectName) {
      RCountDownLatch rCountDownLatch = redisson.getCountDownLatch(objectName);
      return rCountDownLatch;
    }

    /**
     * 获取消息的Topic
     *
     * @param redisson
     * @param objectName
     * @return
     */
    public <M> RTopic getRTopic(RedissonClient redisson, String objectName) {
      RTopic rTopic = redisson.getTopic(objectName);
      return rTopic;
    }
  }

  class jedis {

  }
}
